# -*- coding: utf-8 -*-

__author__	= 'Souheil CHELFOUH <souheil@chelfouh.com>'
__docformat__	= 'plaintext'


from StringIO import StringIO

from Products.CMFCore.utils import getToolByName
from Products.CMFFormController.FormAction import FormActionKey
from Products.Archetypes.Extensions.utils import installTypes
from Products.Archetypes.Extensions.utils import install_subskin
from Products.Archetypes.public import listTypes
# from Products.CMFDynamicViewFTI.migrate import migrateFTIs

from Products.PloneGossip.config import *
from Products.PloneGossip.Extensions.utils import *
# from Products.PloneGossip.workflows import *


def install(self):
    """
    Install PloneGossip
    """
    out = StringIO()

    # Install types
    classes = listTypes(PROJECTNAME)
    installTypes(self, out,
                 classes,
                 PROJECTNAME)


    # Install skin
    install_subskin(self, out, product_globals)
    registerResources(self, out, 'portal_css', STYLESHEETS)
    registerResources(self, out, 'portal_javascripts', JAVASCRIPTS)

    # Migrate FTI, to make sure we get the necessary infrastructure for the
    # 'display' menu to work.
#    migrated = migrateFTIs(self, product=PROJECTNAME)


    # Enable portal_factory
    if len(FACTORY):
        
        factory = getToolByName(self, 'portal_factory')
        types = factory.getFactoryTypes().keys()
        
        for GossipType in FACTORY:
            if GossipType not in types:
                types.append(GossipType)
                
        factory.manage_setPortalFactoryTypes(listOfTypeIds = types)


    # Install workflows
#    installWorkflow(self, 'pgforum_workflow', "PGForum Workflow",
#                    ('PGForum',), isDefault = False)
    
#    installWorkflow(self, 'pgthread_workflow', "PGThread Workflow",
#                    ('PGThread','PGPost'), isDefault = False)


    # Hide types to navigation
    if len(HIDDEN):
        
        portalProperties = getToolByName(self, 'portal_properties')
        navtreeProps = getattr(portalProperties, 'navtree_properties')
        siteProps = getattr(portalProperties, 'site_properties')
    
        for atype in HIDDEN:
            addToListProperty(self, out, navtreeProps, 'parentMetaTypesNotToQuery', atype)
            addToListProperty(self, out, navtreeProps, 'metaTypesNotToList', atype)
            addToListProperty(self, out, siteProps, 'types_not_searched', atype)

    return out.getvalue()


def uninstall(self):
    out = StringIO()

    resetResources(self, out, 'portal_css', STYLESHEETS)
    resetResources(self, out, 'portal_javascripts', JAVASCRIPTS)

    return out.getvalue()
